<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue</title>
  <script src="../js/vue.js"></script>
</head>
<body>
  <div id="app">
    <!-- <div v-mydir></div> -->
    {{username}}
    <br>
    <!-- v-model 双向数据绑定 -->
    <input type="text" v-mydir="username">
  </div>
  <script>
    let myDirective = {
      // 指令第一次绑定到元素时调用
      bind(el,binding,vnode,oldVnode) {
        // console.log(el,binding,vnode,oldVnode,'bind-------');
      },
      // 被绑定元素插入到父节点时调用
      inserted(el,binding,vnode,oldVnode) {
        // 1.将模型中的数据绑定到视图层
        el.value = binding.value
        // 2.将视图层的数据绑定到数据模型对应的字段上
        el.oninput = (e) => {

          vnode.context.$root[binding.expression] = e.target.value

          console.log(vnode.context.$root);
        }
        console.log(el,binding,vnode,oldVnode,'inserted-------');
      },
      // 绑定组件的vnode更新时调用
      update(el,binding,vnode,oldVnode) {
        console.log(el,binding,vnode,oldVnode,'update-------');
      },
      // 绑定组件的vnode以及子vnode更新完毕时调用
      componentUpdated(el,binding,vnode,oldVnode) {
        console.log(el,binding,vnode,oldVnode,'componentUpdated-------');
      },
      // 指令解绑时调用
      unbind(el,binding,vnode,oldVnode) {
        console.log(el,binding,vnode,oldVnode,'unbind-------');
      }
    }
    Vue.directive('mydir', myDirective)
    let vm = new Vue({
      el: '#app',
      data: {
        username: '张三'
      },
      methods: {
        handler(e) {
          console.log(e.target.value);
        }
      }
    })
  </script>
</body>
</html>